package com.regulaforensics.date_reminder;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import java.lang.Math;


public class Alarm extends BroadcastReceiver 
{    
	
	private int alarmHour;
	private int alarmMinute;
	private boolean alarmRunning;
	
	public Alarm()
	{
		alarmRunning = false;
	}
	
     @Override
     public void onReceive(Context context, Intent intent) 
     {   
         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DATE_REMINDER_ALARM");
         SharedPreferences settings = context.getSharedPreferences(DateReminderActivity.PREFS_NAME, 0);

         wl.acquire();
         
		 int alarmHour = settings.getInt(DateReminderActivity.ALARM_HOUR, 7);
		 int alarmMinute = settings.getInt(DateReminderActivity.ALARM_MINUTE, 0);
		 final Calendar current = Calendar.getInstance(); 
		 int currHour = current.get(Calendar.HOUR_OF_DAY);
		 int currMinute = current.get(Calendar.MINUTE);
		 
		 int diffH = Math.abs(alarmHour - currHour);
		 int diffM = Math.abs(alarmMinute - currMinute);
         
		 if(diffH==0 && diffM<=5){
			 //Start service
			 Intent myintent = new Intent(context, ReminderService.class);
			 myintent.putExtra(ReminderService.RERUN, 10);
			 context.startService(myintent);
		 }
         //Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

         wl.release();
     }

 public void SetAlarm(Context context)
 {
	 final Calendar current = Calendar.getInstance(); 
	 int currYear = current.get(Calendar.YEAR);
	 int currMonth = current.get(Calendar.MONTH);
	 int currDay = current.get(Calendar.DAY_OF_MONTH);
	 Calendar calcCalendar = Calendar.getInstance();
	 
	 SharedPreferences settings = context.getSharedPreferences(DateReminderActivity.PREFS_NAME, 0);
	 alarmHour = settings.getInt(DateReminderActivity.ALARM_HOUR, 7);
	 alarmMinute = settings.getInt(DateReminderActivity.ALARM_MINUTE, 0);
		
	 calcCalendar.set(currYear, currMonth, currDay, alarmHour, alarmMinute);
	 
	 AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent i = new Intent(context, Alarm.class);
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
     am.setRepeating(AlarmManager.RTC_WAKEUP,calcCalendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi); //1000 * 30 * 1 Millisec * Second * Minute System.currentTimeMillis()
     alarmRunning = true;
     
 }

 public void CancelAlarm(Context context)
 {
	 Intent intent = new Intent(context, Alarm.class);
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
     alarmManager.cancel(sender);
     alarmRunning = false;
 }
 
 public boolean isAlarmRunning() {
	return alarmRunning;
}
 
}